<?php
namespace Shop\Api;
use Common\Api\Api;
use Shop\Model\OrderRefundModel;
use Shop\Model\OrdersInfoViewModel;
use Shop\Model\OrdersModel;
use Shop\Model\RefundInfoViewModel;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/20
 * Time: 17:38
 */
class OrderRefundApi extends  Api{
    const ADD="Shop/OrderRefund/add";

    const QUERY="Shop/OrderRefund/query";

	const QUERY_NO_PAGING="Shop/OrderRefund/queryNoPaging";

    const SAVE_BY_ID="Shop/OrderRefund/saveById";

    const SAVE="Shop/OrderRefund/save";

    const DELETE="Shop/OrderRefund/delete";

    const GET_INFO ="Shop/OrderRefund/getInfo";

    const QUERY_WITH_ORDER = "Shop/OrderRefund/queryWithOrder";

    const CHANGE_SERVICE_STATUS = "Shop/OrderRefund/changeServerStatus";
    const CHANGE_ORDER_STATUS = "Shop/OrderRefund/changeOrderStatus";

    const QUERY_WITH_COUNT = "Shop/OrderRefund/queryWithCount";

    protected  function _init(){
        $this->model=new OrderRefundModel();
    }

    public function queryWithOrder($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $model = new RefundInfoViewModel();
        if(!is_null($map))    $query = $model->where($map);
        if(false !== $order)  $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);

        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
        // dump($model->getLastSql());exit;
        if (false === $list) return $this->apiReturnErr($model -> getDbError());

        $count = $model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);
        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();
        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));

    }

    /**
     * 修改订单售后状态
     */
    public function changeServerStatus($order_code,$cs_status){

        $map = array(
            'order_code'=>$order_code
        );
        //修改订单的售后状态
        $entity = array(
            'cs_status' => $cs_status
        );
        $result = apiCall(OrdersApi::SAVE,array($map,$entity));
        return $result;
    }
    /**
     * 修改订单状态
     */
    public function changeOrderStatus($order_code,$order_status){

        $map = array(
            'order_code'=>$order_code
        );
        //修改订单的售后状态
        $entity = array(
            'order_status' => $order_status
        );
        $result = apiCall(OrdersApi::SAVE,array($map,$entity));
        return $result;
    }

    public function queryWithCount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
        $query = $this->model;

        $query->join('__ORDERS__ ON __ORDERS__.order_code =__ORDER_REFUND__.order_code','LEFT');

        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> join('__ORDERS__ ON __ORDERS__.order_code =__ORDER_REFUND__.order_code','LEFT') -> where($map) -> count();


        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));
    }

}
